package demo.helloworld.controller.admin;

import act.app.ActionContext;
import act.conf.AppConfig;
import act.controller.Controller;
import demo.helloworld.dto.Statistics;
import demo.helloworld.model.Comments;
import demo.helloworld.model.Contents;
import demo.helloworld.model.Logs;
import demo.helloworld.service.LogService;
import demo.helloworld.service.SiteService;
import demo.helloworld.util.Const;
import demo.helloworld.util.Tool;
import org.osgl.http.H;
import org.osgl.mvc.annotation.Before;
import org.osgl.mvc.annotation.GetAction;

import javax.inject.Inject;

import java.util.List;
import java.util.Map;

import static act.controller.Controller.Util.render;
/**
 * Created by vilen on 2017/2/28.
 */
@SuppressWarnings("unused")
@Controller("admin")
public class IndexController {

    @Inject
    private SiteService siteService;
    @Inject
    private LogService logService;
    @Inject
    private H.Session session;


    @GetAction
    public void home() {
        boolean installed = true;
        render(installed);
    }


    /**
     * 仪表盘
     */
    @GetAction("index")
    public void index() {
        List<Comments> comments = siteService.recentComments(5);
        List<Contents> articles = siteService.recentContents(5);
        Statistics statistics = siteService.getStatistics();
        List<Logs> logs = logService.getLogs(1, 20);
        String siteUrl = (String) Const.options.get("site_url");
        render(comments,articles,statistics,logs,siteUrl);
    }

    @GetAction("setting")
    public void setting() {
        // todo 添加 site_theme 字段,实现的不好,其实 options 里面已经有该字段
        String siteTheme = (String) Const.options.get("site_theme");
        render(siteTheme);
    }

    @Before
    public void before(ActionContext actionContext) {
        actionContext.renderArg("options", Const.options);
        // TODO: 2017/3/10 这里暂时存放 session
        String userEmail = "chhx4567@gmail.com";
        String userEmailUrl = Tool.gravatar(userEmail);
        session.put("userEmail", userEmailUrl);
    }



}
